package com.hutao.medical.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.medical.common.util.Constants;
import com.hutao.medical.common.util.DateUtil;
import com.hutao.medical.common.util.Result;
import com.hutao.medical.mapper.audit.AuditRecordDetailMapper;
import com.hutao.medical.pojo.audit.AuditRecord;
import com.hutao.medical.pojo.audit.AuditRecordDetail;
import com.hutao.medical.service.AuditRecordDetailService;
import com.hutao.medical.service.AuditRecordService;


@Transactional
@Service
public class AuditRecordDetailServiceImpl implements AuditRecordDetailService{
	
	@Autowired
	private AuditRecordDetailMapper auditRecordDetailMapper;

	@Autowired
	private AuditRecordService auditRecordService;
	
	@Override
	public Result insertAuditRecordDetail(String firstGuid, String operationName, AuditRecord auditRecord, 
				Map<String, Object>firstLevelMedical) throws Exception{
		List<AuditRecordDetail> auditRecordDetailList = auditRecord.getAuditRecordDetailList();
		
		if (null != auditRecordDetailList && auditRecordDetailList.size() > 0) {
			
			if(auditRecordService.findAuditRecordCountByFirstGuid(firstGuid) == 0) {
				AuditRecord newAuditRecord = new AuditRecord();
				newAuditRecord.setFirstGuid(firstGuid);
				newAuditRecord.setName(firstLevelMedical.get("name")==null?null:(String)firstLevelMedical.get("name"));
				newAuditRecord.setIdCard(firstLevelMedical.get("idCard")==null?null:(String)firstLevelMedical.get("idCard"));
				newAuditRecord.setMrId(firstLevelMedical.get("mrId")==null?null:(String)firstLevelMedical.get("mrId"));
				newAuditRecord.setOutOfficeName(firstLevelMedical.get("officeName")==null?null:(String)firstLevelMedical.get("officeName"));
				newAuditRecord.setOutHospitalType(firstLevelMedical.get("outHospitalTypeName")==null?null:(String)firstLevelMedical.get("outHospitalTypeName"));
				newAuditRecord.setOperator(operationName);
				newAuditRecord.setOutHospitalDate(firstLevelMedical.get("outHospitalDate")==null?null:DateUtil.strToDate("yyyy-MM-dd",(String) firstLevelMedical.get("outHospitalDate")));
				auditRecordService.insertAuditRecord(newAuditRecord);
				
				
			}
			
			//添加审核内容详情
			for(int i = 0; i < auditRecordDetailList.size(); i++) {
				if(auditRecordDetailMapper.findAuditDetailRecordCountByFirstGuidAndAuditStandardId(firstGuid, auditRecordDetailList.get(i).getAuditingStandardId())==0){
					
					AuditRecordDetail auditRecordDetail = new AuditRecordDetail();
					auditRecordDetail.setFirstGuid(firstGuid);
					auditRecordDetail.setAuditingStandardId(auditRecordDetailList.get(i).getAuditingStandardId());
					auditRecordDetail.setRemark(auditRecordDetailList.get(i).getRemark());
					auditRecord.setCreateTime(auditRecordDetailList.get(i).getCreateTime());
					auditRecordDetail.setStatus(Constants.NO_UPDATE);
					auditRecordDetailMapper.insertAuditRecordDetail(auditRecordDetail);
				}
			}
			return new Result(true, "添加成功!");
		}
		
		
		return new Result(false, "添加失败!");
	}
	
	/**
	 * 拼装保存参数
	 * @param auditDicId
	 * @return
	 */
	public AuditRecord appendAuditRecord(Integer auditDicId){
		AuditRecord auditRecord = new AuditRecord();
		List<AuditRecordDetail> auditRecordDetailList = new ArrayList<>();
		AuditRecordDetail auditRecordDetail = new AuditRecordDetail();
		Map<String, Object> auditDic = auditRecordDetailMapper.findAuditStandardById(auditDicId);
		if(MapUtils.isNotEmpty(auditDic)){
			auditRecordDetail.setAuditingStandardId(auditDicId);
			auditRecordDetail.setRemark(auditDic.get("remark")==null?null:(String)auditDic.get("remark"));
			auditRecordDetail.setCreateTime(new Date());
			auditRecordDetailList.add(auditRecordDetail);
		}
		auditRecord.setAuditRecordDetailList(auditRecordDetailList);
		return auditRecord;
	}

	@Transactional(readOnly = true)
	@Override
	public List<AuditRecordDetail> findAuditRecordDetailListByFirstGuid(String firstGuid) {
		List<AuditRecordDetail> auditRecordDetailList = auditRecordDetailMapper.findAuditRecordDetailListByFirstGuid(firstGuid);
		return auditRecordDetailList;
	}

	@Override
	public int updateAuditRecordDetail(AuditRecordDetail auditRecordDetail) {
		return auditRecordDetailMapper.updateAuditRecordDetail(auditRecordDetail);
	}

	@Transactional(readOnly = true)
	@Override
	public AuditRecordDetail findAuditRecordDetailListById(Integer id) {
		return auditRecordDetailMapper.findAuditRecordDetailListById(id);
	}

	@Override
	public int updateStatusById(Integer id) {
		return auditRecordDetailMapper.updateStatusById(id);
	}

	@Transactional(readOnly = true)
	@Override
	public long findCountByFristGuid(String firstGuid) {
		return auditRecordDetailMapper.findCountByFristGuid(firstGuid);
	}

	@Override
	public List<Map<String, Object>> findAuditDetailByFirstGuid(String firstGuid) {
		return auditRecordDetailMapper.findAuditDetailByFirstGuid(firstGuid);
	}

	@Override
	public Map<String, Object> findAuditStandardById(Integer auditDicId) {
		return auditRecordDetailMapper.findAuditStandardById(auditDicId);
	}


	
	
	
	

}
